# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include Helpers

  def nav_rating_tab action_name
    if action_name == "index"
      "投票"
    else
      "<a href=\"/peipeidui\">rating</a>"
    end
  end 

  def nav_couples_tab action_name
    if action_name == "couples"
      "热度榜"
    else
      "<a href=\"/peipeidui/couples\">热度榜</a>"
    end
  end   

   # to make a linking couple
  def xiaonei_couple(couple,pic_options={:size => "tiny"})
    pic1 = xn_profile_pic_with_id couple.first_user_id.to_s , pic_options 
    pic2 = xn_profile_pic_with_id couple.second_user_id.to_s , pic_options 
    content_tag(:a, content_tag(:span, pic1 + "&nbsp;" + pic2), 
                {:href => "/peipeidui/couple_link?couple_link=#{couple.id.to_s}"})
  end

  # 改写了 distance_of_time_in_words
  def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round
    distance_in_seconds = ((to_time - from_time).abs).round

    case distance_in_minutes
    when 0..1
      return (distance_in_minutes == 0) ? '1秒钟之前' : '1秒钟之前' unless include_seconds
      case distance_in_seconds
      when 0..4   then '5秒钟之前'
      when 5..9   then '10秒钟之前'
      when 10..19 then '10秒钟之前'
      when 20..39 then '半分钟之前'
      when 40..59 then '1分钟之前'
      else             '1 分钟之前'
      end

    when 2..44           then "#{distance_in_minutes} 分钟之前"
    when 45..89          then '1小时之前'
    when 90..1439        then "#{(distance_in_minutes.to_f / 60.0).round} 小时之前"
    when 1440..2879      then '1 天之前'
    when 2880..43199     then "#{(distance_in_minutes / 1440).round} 天之前"
    when 43200..86399    then '1 个月之前'
    when 86400..525599   then "#{(distance_in_minutes / 43200).round} 月之前"
    when 525600..1051199 then '1 年之前'
    else                      "#{(distance_in_minutes / 525600).round} 年之前"
    end
  end

end
